package com.oms.app.shop.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.oms.app.shop.dao.IShopDAO;
import com.oms.app.shop.model.Shop;
import com.oms.app.shop.model.ShopMy;
import com.oms.core.dao.impl.GeneralDAO;
import com.oms.core.model.Query;
import com.oms.util.DataDeal;
import com.oms.util.DateUtil;

/**
 * @author Loong_Mit
 * @date Sep 1, 2012 6:32:12 PM
 */

public class ShopDAOImpl extends GeneralDAO<Shop, String> implements IShopDAO {

	@Override
	public List<Shop> getShopList(Query query) {
		return this.queryForList("getAll", query);
	}

	@Override
	public List<Shop> getMyApplyList(String creator) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("creator", creator);
		return this.queryForList("getMyApplyList", param);
	}
	
	@Override
	public boolean addShop(Shop shopForm){
		try {
			shopForm.setCreatertime(DataDeal.ObjCDate(DateUtil.getCurrentFullString(), "yyyy-MM-dd HH:mm:ss"));
			this.insert(shopForm);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public boolean deleteShop(String PK) {
		try {
			this.delete("delete", PK);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public boolean updateShop(Shop shopForm) {
		try {
			this.update("update", shopForm);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	@Override
	public boolean isOrderFood(String PK) {
		int ct = this.getCount("isOrderFood", PK);
		return (ct > 0);
	}
	
	@Override
	public boolean changeShopStatus(String PK, String status) {
		try {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("id", PK);
			param.put("status", status);
			this.update("changeShopStatus", param);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public List<Map<String, Object>> getShopList(Map<String, Object> m) {
		return (List<Map<String,Object>>) this.getList("getShopList", m);
	}
	
	@Override
	public List<Map<String, Object>> getMyShopList(Map<String, Object> m) {
		return (List<Map<String,Object>>) this.getList("getMyShopList", m);
	}
	
	@Override
	public List<Map<String, Object>> getMyOrderList(Map<String, Object> map) {
		return (List<Map<String, Object>>) this.getList("getMyOrderList", map);
	}
	
	@Override
	public List<Map<String, Object>> getMyOrderDetailList(Map<String, Object> map) {
		return (List<Map<String, Object>>) this.getList("getMyOrderDetailList", map);
	}
	
	@Override
	public boolean changeOrderDetailStatus(String key, String status) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("detail_id", key);
			map.put("status", status);
			this.update("changeOrderDetailStatus", map);
			return true;
		} catch (Exception e) {
			return false;
		}
	}
	
	@Override
	public boolean deleteOrderDetails(String keyIds) {
		try {
			if (keyIds.length() > 0) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("detailIds", keyIds);
				this.update("deleteOrderDetails", map);
				return true;
			}
			return false;
		} catch (Exception e) {
			return false;
		}
	}
	
	@Override
	public boolean saveApplyShop(ShopMy shopMy){
		try {
			this.insert("inserShopMy",shopMy);
			return false;
		} catch (Exception e) {
			return false;
		}
	}

	@Override
	public List<Map<String, Object>> getApplyUserList(String shopid) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("shopid", shopid);
		return (List<Map<String, Object>>) this.getList("getApplyUserList", map);
	}
	
	@Override
	public boolean isAppliedShop(Long shopId, Long usId) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("shopid", shopId);
			map.put("usid", usId);
			List<Map<String, Object>> list = this.getList("getIsAppliedShop", map);
			return (list.size() > 0);
		} catch (Exception e) {
			return false;
		}
	}
}